package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.utils.glide.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by 徐嘉健 on 2018/10/31.
 */

public class PromotionNewAdapter extends RecyclerView.Adapter<PromotionNewAdapter.PromotionActHolder> {
    private final Context context;
    private List<GoodsInfo> list;
    private String name;
    ISetOnNewSecondClick onSecondItemClickListener;

    public void setOnSecondItemClickListener(ISetOnNewSecondClick onSecondItemClickListener) {
        this.onSecondItemClickListener = onSecondItemClickListener;
    }

    public PromotionNewAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    @Override
    public PromotionActHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PromotionActHolder(LayoutInflater.from(context).inflate(R.layout.item_right_checkview, parent, false));
    }

    @Override
    public void onBindViewHolder(final PromotionActHolder viewHolderSecond, final int position) {
        try {
            final GoodsInfo goodsData = list.get(position);
            viewHolderSecond.tv_goods_name.setText(goodsData.getGoods_name() + "");
            //分类名称是否显示
            if (position == 0) {
                viewHolderSecond.lin_goods_classify.setVisibility(VISIBLE);
            } else {
                viewHolderSecond.lin_goods_classify.setVisibility(GONE);
            }
            viewHolderSecond.tv_goods_classify.setText(name);
            GlideImgManager.getLoadImg(context,goodsData.getGoods_img(),viewHolderSecond.iv_goods_img);
            viewHolderSecond.tv_repertory.setText("库存："+goodsData.getInventory_keep());
            if (goodsData.isSelected()) {
                viewHolderSecond.box_check.setBackgroundResource(R.drawable.box_select_s);
            }else {
                viewHolderSecond.box_check.setBackgroundResource(R.drawable.box_select_n);
            }
            viewHolderSecond.box_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onSecondItemClickListener && goodsData.isSelected()){
                        onSecondItemClickListener.onCheckChange(!goodsData.isSelected(),position);
                    }else {
                        ToastUtils.showToast("该商品未进行批次修改，不可选中");
                    }
                }
            });
            //item的点击事件
            viewHolderSecond.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSecondItemClickListener != null) {
                        onSecondItemClickListener.setOnSecondClick(v, goodsData);
                    }
                }
            });
        } catch (Exception e) {
            Trace.e("异常=" + e);
        }
    }

    public List<GoodsInfo> getList(){
        return list;
    }

    /**
     * 更新列表是否被选中
     * @param isSelect  是否选中
     * @param position  改变的位置
     */
    public void updateRightData(boolean isSelect, int position) {
        list.get(position).setSelected(isSelect);
        notifyItemChanged(position);
    }

    public interface ISetOnNewSecondClick {
        void setOnSecondClick(View v, GoodsInfo secondGoods);

        void onCheckChange(boolean isSelect, int position);
    }

    /**
     * 设置数据
     *
     * @param list
     */
    public void setRightListData(List<GoodsInfo> list, int pageNo, String name) {
        this.name = name;
        if (pageNo == 1) {
            this.list = list;
            notifyDataSetChanged();
        }else {
            this.list.addAll(list);
            notifyItemRangeChanged(this.list.size() - list.size(), list.size());
        }

    }

    @Override
    public int getItemCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    class PromotionActHolder extends RecyclerView.ViewHolder {
        TextView tv_goods_name;//商品名称
        TextView tv_repertory;//库存
        ImageView iv_goods_img;//商品图片
        TextView tv_goods_classify;//商品分类

        LinearLayout lin_goods_classify;//分类标题
        ImageView box_check;
        View view_line;

        public PromotionActHolder(View itemView) {
            super(itemView);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_goods_classify = (TextView) itemView.findViewById(R.id.tv_goods_classify);
            tv_repertory = (TextView) itemView.findViewById(R.id.tv_repertory);
            box_check = (ImageView) itemView.findViewById(R.id.box_check);
            iv_goods_img = (ImageView) itemView.findViewById(R.id.iv_goods_img);
            lin_goods_classify = (LinearLayout) itemView.findViewById(R.id.lin_goods_classify);
            view_line = (View) itemView.findViewById(R.id.view_line);
        }
    }
}


